package com.mewpipe.servlet;

import java.io.IOException;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.mewpipe.dao.CommentDao;
import com.mewpipe.dao.DaoFactory;
import com.mewpipe.dao.LnkUserVideoDao;
import com.mewpipe.dao.UserDao;
import com.mewpipe.dao.VideoDao;
import com.mewpipe.entity.Comment;
import com.mewpipe.entity.User;
import com.mewpipe.entity.Video;


@WebServlet("/play")
public class playServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;
	private VideoDao videoDao;
	private LnkUserVideoDao lnkDao;
	private Video video;
	private User userOwner;
	private UserDao userDao;
	private long userOwnerId;
	private CommentDao commentDao;
       
  @Override
public void init() throws ServletException {
	  videoDao = DaoFactory.getInstance().getVideoDao();
		lnkDao = DaoFactory.getInstance().getLnkDao();
		userDao = DaoFactory.getInstance().getUserDao();
		commentDao = DaoFactory.getInstance().getCommentDao();
	  
}
	
	protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		String code = request.getParameter( "id" );
			int idVideo = Integer.parseInt(videoDao.getVideoIdByCode(code));
			System.out.println("code : "+ code);
			System.out.println("idVideo : "+idVideo);
		 video = videoDao.getVideoInfo(idVideo);
		 System.out.println(video.getNom());
		 userOwnerId = lnkDao.findUserByVideo(idVideo);
		 userOwner = userDao.getUserInfo(userOwnerId);
		 System.out.println(userOwner.getPseudo());
		 
		 List<Comment> comments = null;
		 comments = commentDao.getAllCommentByVideoId(idVideo);
		 
		 request.setAttribute("video", video);
		 request.setAttribute("userOwner", userOwner);
		 request.setAttribute("ListComment",comments);
		 
		 request.getRequestDispatcher("/play.jsp").forward(request, response);
		
		
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse response)
	 */
	protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
	}

}
